package com.mission.schedule.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.adapter.EditTagAdapter;
import com.mission.schedule.adapter.QDPXAdapter;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.TagCommandBean;
import com.mission.schedule.bean.qd606.CLInventoryListBean;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.service.UpdataTagService;
import com.mobeta.android.dslv.DragSortListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class QDpxActivity extends BaseActivity implements OnClickListener {

	@ViewResId(id = R.id.new_dtl_back)
	private LinearLayout new_dtl_back;
	@ViewResId(id = R.id.top_ll_right)
	private RelativeLayout top_ll_right;
	@ViewResId(id = R.id.tag_lv)
	private DragSortListView tag_lv;

	Context context;

	App app = App.getDBcApplication();
	List<NoteTitleDetailBean.ListBean> mList = new ArrayList<>();
	QDPXAdapter adapter = null;
	int stylee = 0;

	@Override
	protected void setListener() {
		new_dtl_back.setOnClickListener(this);
		top_ll_right.setOnClickListener(this);
	}

	@Override
	protected void setContentView() {
		setContentView(R.layout.qdpx);
	}

	@Override
	protected void init(Bundle savedInstanceState) {
		context = this;
		tag_lv.setDropListener(onDrop);
		stylee = getIntent().getIntExtra("stylee",0);
		// tag_lv.setRemoveListener(onRemove);
		loadData();
	}

	private void loadData() {
		mList = (List<NoteTitleDetailBean.ListBean>) getIntent().getSerializableExtra("list");
		adapter = new QDPXAdapter(context, mList, handler, R.layout.adapter_qdpx,stylee);
		tag_lv.setAdapter(adapter);
	}

	@Override
	protected void setAdapter() {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.new_dtl_back:
			this.finish();
			break;
		case R.id.top_ll_right:
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putSerializable("pxlist", (Serializable) adapter.mlList);
			intent.putExtras(bundle);
			setResult(Activity.RESULT_OK, intent);
			this.finish();
			break;
		default:
			break;
		}
	}

	private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
		@Override
		public void drop(int from, int to) {
			if (from != to) {
				NoteTitleDetailBean.ListBean item = (NoteTitleDetailBean.ListBean) adapter.getItem(from);
				adapter.remove(item);
				adapter.insert(item, to);
				adapter.notifyDataSetChanged();
			}
		}
	};

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
		};
	};

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		loadData();
		adapter.notifyDataSetChanged();
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
